package com.atguigu.educenter.service.impl;

import com.atguigu.common_uitls.JwtUtils;
import com.atguigu.common_uitls.MD5;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exception.guliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author wzh
 * @since 2022-10-03
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public String login(UcenterMember ucenterMember) {
        String password = ucenterMember.getPassword();
        String mobile = ucenterMember.getMobile();
        QueryWrapper<UcenterMember> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        UcenterMember member = baseMapper.selectOne(queryWrapper);
        if (member==null||password==null||mobile==null){
            throw new guliException(20001,"登录失败");
        }
        if (!MD5.encrypt(password).equals(member.getPassword())){
            throw new guliException(20001,"密码不正确");
        }
        if (member.getIsDisabled()){
            throw new guliException(20001,"该用户已被禁用");
        }
        String jwtToken = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String nickname = registerVo.getNickname();
        if (code==null||password==null||mobile==null||nickname==null){
            throw new guliException(20001,"注册失败");
        }
        if (!redisTemplate.opsForValue().get(mobile).equals(code)){
            throw new guliException(20001,"注册失败,验证码不正确");
        }
        QueryWrapper<UcenterMember> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(queryWrapper);
        if (count>0){
            throw new guliException(20001,"注册失败,手机号已被使用");
        }
        UcenterMember member=new UcenterMember();
        member.setPassword(MD5.encrypt(password));
        member.setMobile(mobile);
        member.setAvatar("");
        member.setIsDeleted(false);
        member.setNickname(nickname);
        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer countRegister(String day) {
        Integer count=baseMapper.countRegister(day);
        return count;
    }
}
